﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LuckyLoot.ServiceFacade.Implementation;
using LuckyLoot.ServiceFacade.Contracts;
using LuckyLoot.Presentation.BasePage;
using LuckyLoot.Common.Validation;
using LuckyLoot.Domain.Entities.People;

namespace LuckyLoot.Presentation
{
    public partial class CustomerPage : LuckyLootPage
    {
        private const string QueryStringCustomerID = "CustomerID";


        private ICustomerManagementService _customerService;
        public ICustomerManagementService CustomerService
        {
            get
            {
                if (_customerService == null)
                    _customerService = new CustomerManagementService();

                return _customerService;
            }
        }
        
        
        public int CustomerID
        {
            set
            {
                this.ViewState[QueryStringCustomerID] = value;
            }
            get
            {
                int viewStateTypedCustomerID;

                if (this.ViewState[QueryStringCustomerID] != null)
                {
                    if (Int32.TryParse(this.ViewState[QueryStringCustomerID].ToString(), out viewStateTypedCustomerID))
                        return viewStateTypedCustomerID;
                }

                return 0;
            }
        }

        public bool HasSelectedRecord
        {
            get
            {
                return CustomerID > 0;
            }
        }

        private void SetCommandsVisibility()
        {

        }

        private void SelectRecord(string id)
        {
            if (!string.IsNullOrWhiteSpace(id))
            {
                int intID;
                if (Int32.TryParse(id, out intID))
                    SelectRecord(intID);
            }

        }

        private void SelectRecord(int id)
        {
            ValidState.Requires<ArgumentException>(id > 0);
            var customer = this.CustomerService.GetCustomer(id);

            SelectRecord(customer);
        }

        private void SelectRecord(LuckyLoot.Domain.Entities.People.Customer customer)
        {
            this.customerName.Text = customer.Name;
            this.customerPhone.Text = customer.Phone;
            this.customerEmail.Text = customer.Email;
            this.customerFacebook.Text = customer.Facebook;

            this.CustomerID = customer.Id;
        }

        private void ClearRecordSelection()
        {
            this.CustomerID = 0;
            this.ClearFields();
        }

        private void ClearFields()
        {
            this.customerName.Text = string.Empty;
            this.customerPhone.Text = string.Empty;
            this.customerEmail.Text = string.Empty;
            this.customerFacebook.Text = string.Empty;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    var customerIDString = HttpContext.Current.Request.QueryString[QueryStringCustomerID];
                    this.SelectRecord(customerIDString);
                }

                this.SaveCustomer.Click += new EventHandler(SaveCustomerClick);
                this.SetCommandsVisibility();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                MasterPage.ShowErrorMessage(ex.Message);
            }
        }

        public void SaveCustomerClick(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(customerName.Text))
                throw new ArgumentNullException("customerName", "É obrigatório informar o nome do cliente.");

            Customer customer;
            if (this.HasSelectedRecord)
                customer = this.CustomerService.UpdateCustomer(CustomerID, customerName.Text, customerPhone.Text, customerEmail.Text, customerFacebook.Text);
            else
                customer = this.CustomerService.CreateCustomer(customerName.Text, customerPhone.Text, customerEmail.Text, customerFacebook.Text);

            this.SelectRecord(customer);
            this.SetCommandsVisibility();
            MasterPage.ShowSuccessMessage("Cliente salvo com sucesso!");
            this.BindGrid();
        }

        private void BindGrid()
        {
            var customers = this.CustomerService.GetAllCustomers();

            if (customers.Count() > 0)
            {
                this.customersGrid.DataSource = customers;
                this.customersGrid.DataBind();
                this.customersGrid.Visible = true;
                this.customersLabel.Visible = true;
            }
        }
    }
}